<?php 
namespace Think\Template\TagLib;
use Think\Template\TagLib;
class Dc extends TagLib{
	protected $tags=array(
		'channel'    =>  array('attr'=>'cid,row','level'=>3),
		'article'    =>  array('attr'=>'aid,row,cid,pic','level'=>3),


		);
	//栏目导航
	public function _channel($tag,$content){
		$cid=$tag['cid'];
		$row=$tag['row'];
		$parseStr=<<<str
		<?php
		\$Model = M('category');
		\$result = \$Model->where("pid=$cid")->limit($row)->select();
		foreach(\$result as \$field):
			\$field['url']=U('Category',array('cid'=>\$field['cid']));

		?>
str;
	$parseStr.=$content;
	$parseStr.='<?php endforeach;?>';
	return $parseStr;

	}
	//文章
	public function _article($tag,$content){
		$cid=$tag['cid'];
		$id=explode(',', str_replace(' ', '', $cid));
		$d=$id[0];
		$mid=M('category')->where("cid=$d")->getField('mid');
		$table=M('model')->where("mid=$mid")->getField('m_table_name');
		$Model = M("$table");
		$row=isset($tag['row'])?" limit {$tag['row']}":'';
		$pic=isset($tag['pic'])?$tag['pic']:0;
		$parseStr='';
		if($pic){
			$aid=isset($tag['aid'])?"and c.contid={$tag['aid']}":'';
			$flag=isset($tag['flag'])?" and c.flag like '%".$tag['flag']."%'":'';
			$parseStr.=<<<str
		<?php
			\$sql="SELECT * FROM d_$table AS c JOIN d_pic AS p ON c.contid=p.contid WHERE c.cid in ($cid) $aid $flag $row";
			\$result=M()->query(\$sql);
			foreach(\$result as \$field):
			\$field['url']=U('content',array('contid'=>\$field['contid']));
		?>
str;
		}else{
			$aid=isset($tag['aid'])?"and contid={$tag['aid']}":'';
			$flag=isset($tag['flag'])?" and flag like '%".$tag['flag']."%'":'';
			$parseStr.=<<<str
		<?php
			\$sql="SELECT * FROM d_$table WHERE cid in ($cid) $aid $flag $row";	
			\$result=M()->query(\$sql);
			foreach(\$result as \$field):
			\$field['url']=U('content',array('contid'=>\$field['contid']));
			\$field['thumb']="__ROOT__/".\$field['thumb'];
		?>
str;
		}
		
		$parseStr.=$content;
		$parseStr.='<?php endforeach;?>';
		return $parseStr;
	}

	// public function _pic($tag,$content){

	// }





}




 ?>